/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator.datastore.genericdao.hibernate;

import com.googlecode.armycreator.datastore.genericdao.GenericDao;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;

import java.util.List;

/**
 * Hibernate implementation for the {@link GenericDao} interface.
 * 
 * @author <a href="mailto:daniel.mfreitas@gmail.com">Daniel Freitas</a>
 * @param <T>
 *            The domain object this DAO is responsible for.
 */
public class HibernateGenericDao<T> implements GenericDao<T> {

    private final Class<T> domainObjectClass;
    private final SessionFactory sessionFactory;

    /**
     * Creates a new Hibernate generic DAO.
     * 
     * @param domainObjectClass
     *            The domain object {@link Class} this DAO is responsible for.
     * @param sessionFactory
     *            The hibernate {@link SessionFactory} to use to get hibernate sessions.
     */
    public HibernateGenericDao(Class<T> domainObjectClass, SessionFactory sessionFactory) {
        this.domainObjectClass = domainObjectClass;
        this.sessionFactory = sessionFactory;
    }

    /**
     * @see com.googlecode.armycreator.datastore.genericdao.GenericDao#findAll()
     */
    @Override
    public List<T> findAll() {
        return findByCriteria();
    }

    /**
     * @see com.googlecode.armycreator.datastore.genericdao.GenericDao#findByExample(java.lang.Object,
     *      java.lang.String[])
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<T> findByExample(T exampleObject, String... excludeProperty) {
        Criteria c = sessionFactory.getCurrentSession().createCriteria(domainObjectClass);
        Example example = Example.create(exampleObject);

        for (String property : excludeProperty) {
            example.excludeProperty(property);
        }

        c.add(example);

        return c.list();
    }

    /**
     * @see com.googlecode.armycreator.datastore.genericdao.GenericDao#findById(java.lang.Long)
     */
    @SuppressWarnings("unchecked")
    @Override
    public T findById(Long id) {
        return (T) sessionFactory.getCurrentSession().get(domainObjectClass, id);
    }

    /**
     * @see com.googlecode.armycreator.datastore.genericdao.GenericDao#makePersistent(java.lang.Object)
     */
    @Override
    public T makePersistent(T o) {
        sessionFactory.getCurrentSession().saveOrUpdate(o);
        return o;
    }

    /**
     * @see com.googlecode.armycreator.datastore.genericdao.GenericDao#makeTransient(java.lang.Object)
     */
    @Override
    public void makeTransient(T o) {
        sessionFactory.getCurrentSession().delete(o);
    }

    // Extracted methods
    // -----------------------------------------------------------------------------------------------------------------
    @SuppressWarnings("unchecked")
    private List<T> findByCriteria(Criterion... criterion) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(domainObjectClass);

        for (Criterion c : criterion) {
            criteria.add(c);
        }

        return criteria.list();
    }
}
